/**
 * 作者: wgx
 * 日期: 2025-03-13 17:15:49
 * 备注: 玩家物资争夺被打防守记录(匹配战斗生成)
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserSnatchRecord;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserSnatchRecordModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // ID
    protected long userId; // 用户ID
    protected long enemyId; // 来打我的敌人的id
    protected String userName = ""; // 对方的username
    protected int level; // 对方的level
    protected int koNum; // 击败我方的干员人数
    protected int isKoAll; // 是否全部击败 (1-全部击败)
    protected int subNum; // 我方物质减少的数量
    protected long addTime; // 被打的时间

    public void reset() {
    }

    protected String getKey() {
        return "UserSnatchRecord." + id;
    }

    protected String getListKey() {
        return "UserSnatchRecord." + userId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_snatch_record"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("userId", userId); // 用户ID
        obj.put("enemyId", enemyId); // 来打我的敌人的id
        obj.put("userName", userName); // 对方的username
        obj.put("level", level); // 对方的level
        obj.put("koNum", koNum); // 击败我方的干员人数
        obj.put("isKoAll", isKoAll); // 是否全部击败 (1-全部击败)
        obj.put("subNum", subNum); // 我方物质减少的数量
        obj.put("addTime", addTime); // 被打的时间
        return obj;
    }

    public PBUserSnatchRecord.Builder takeInitBuilder() {
        PBUserSnatchRecord.Builder sb = PBUserSnatchRecord.newBuilder();
        if (enemyId != 0) {
            sb.setEnemyId(enemyId);
        }
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (level != 0) {
            sb.setLevel(level);
        }
        if (koNum != 0) {
            sb.setKoNum(koNum);
        }
        if (isKoAll != 0) {
            sb.setIsKoAll(isKoAll);
        }
        if (subNum != 0) {
            sb.setSubNum(subNum);
        }
        if (addTime != 0) {
            sb.setAddTime(addTime);
        }
        return sb;
    }

    public PBUserSnatchRecord.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserSnatchRecord.Builder sb = PBUserSnatchRecord.newBuilder();
        if (isFieldUpdate("enemyId")) {
            sb.setEnemyId(enemyId);
        }
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("level")) {
            sb.setLevel(level);
        }
        if (isFieldUpdate("koNum")) {
            sb.setKoNum(koNum);
        }
        if (isFieldUpdate("isKoAll")) {
            sb.setIsKoAll(isKoAll);
        }
        if (isFieldUpdate("subNum")) {
            sb.setSubNum(subNum);
        }
        if (isFieldUpdate("addTime")) {
            sb.setAddTime(addTime);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getEnemyId() {
        return enemyId;
    }

    public void putEnemyId(long enemyId) {
        if (this.enemyId != enemyId) {
            this.enemyId = enemyId;
            updateRedisHashField(getKey(), "enemyId", this.enemyId);
        }
    }

    public void addEnemyId(long value) {
        this.enemyId += value;
        updateRedisHashField(getKey(), "enemyId", this.enemyId);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public int getLevel() {
        return level;
    }

    public void putLevel(int level) {
        if (this.level != level) {
            this.level = level;
            updateRedisHashField(getKey(), "level", this.level);
        }
    }

    public void addLevel(int value) {
        this.level += value;
        updateRedisHashField(getKey(), "level", this.level);
    }

    public int getKoNum() {
        return koNum;
    }

    public void putKoNum(int koNum) {
        if (this.koNum != koNum) {
            this.koNum = koNum;
            updateRedisHashField(getKey(), "koNum", this.koNum);
        }
    }

    public void addKoNum(int value) {
        this.koNum += value;
        updateRedisHashField(getKey(), "koNum", this.koNum);
    }

    public int getIsKoAll() {
        return isKoAll;
    }

    public void putIsKoAll(int isKoAll) {
        if (this.isKoAll != isKoAll) {
            this.isKoAll = isKoAll;
            updateRedisHashField(getKey(), "isKoAll", this.isKoAll);
        }
    }

    public void addIsKoAll(int value) {
        this.isKoAll += value;
        updateRedisHashField(getKey(), "isKoAll", this.isKoAll);
    }

    public int getSubNum() {
        return subNum;
    }

    public void putSubNum(int subNum) {
        if (this.subNum != subNum) {
            this.subNum = subNum;
            updateRedisHashField(getKey(), "subNum", this.subNum);
        }
    }

    public void addSubNum(int value) {
        this.subNum += value;
        updateRedisHashField(getKey(), "subNum", this.subNum);
    }

    public long getAddTime() {
        return addTime;
    }

    public void putAddTime(long addTime) {
        if (this.addTime != addTime) {
            this.addTime = addTime;
            updateRedisHashField(getKey(), "addTime", this.addTime);
        }
    }

    public void addAddTime(long value) {
        this.addTime += value;
        updateRedisHashField(getKey(), "addTime", this.addTime);
    }

}